﻿//
// Created by work on 24-6-6.
//

#include "CRtModelYxInfo.h"

std::map<CRtModelYxInfo::EModelYxCol, QString> CRtModelYxInfo::rtModelYxColToStringMap = {
    {CRtModelYxInfo::EValues, "valuesmap"}
};

std::map<CRtModelYxInfo::EModelYxCol, QString> CRtModelYxInfo::yxColFieldsNameMap = {
    {CRtModelYxInfo::EValues,"值映射"}
};

std::map<CRtModelYxInfo::EModelYxCol, CValue::EValueType> CRtModelYxInfo::rtYxDataTypeByField = {
    {CRtModelYxInfo::EValues, CValue::IOT_String}
};

CRtModelYxInfo::CRtModelYxInfo(QObject *parent) : CRtModelSigBase(YX_COL_SIZE, parent) {
    m_enPluginEdit.insert(yxColFieldName(YX_ENUM::EValues), true);
    m_fieldPluginMap.insert(yxColFieldName(YX_ENUM::EValues), "yxvalueeditplugin");
}

QJsonArray CRtModelYxInfo::valuesToJson() const
{
    QJsonArray arr;
    for(const auto& it : values) {
        arr.append(it.toJson());
    }
    return arr;
}

void CRtModelYxInfo::valuesFromJson(QJsonArray json)
{
    values.clear();
    if(json.isEmpty())
        return;

    for(auto obj : json) {
        SYxValue value;
        value.fromJson(obj.toObject());
        values.push_back(value);
    }
}

QString CRtModelYxInfo::getFieldNameByCol(int col) {
    if(col < 0 || col > (YX_COL_SIZE-1))
        return "";

    if(col < SIGBASE_COL_SIZE)
        return CRtModelSigBase::getFieldNameByCol(col);

    return yxColFieldName(static_cast<EModelYxCol>(col));
}

QVariant CRtModelYxInfo::getFieldValueByFielddName(const QString &name) {
    if(name.isEmpty())
        return QVariant();

    if(yxColFieldName(YX_ENUM::EValues) == name) {
        QJsonObject obj;
        obj[yxFieldName(YX_ENUM::EValues)] = valuesToJson();
        return QString(QJsonDocument(obj).toJson(QJsonDocument::Compact));
    } else {
        return CRtModelSigBase::getFieldValueByFielddName(name);
    }
}

void CRtModelYxInfo::setFieldValueByCol(int col, const QVariant &value)
{

}

QJsonObject CRtModelYxInfo::toJson() {
    QJsonObject json = CRtModelSigBase::toJson();
    json[yxFieldName(YX_ENUM::EValues)] = valuesToJson();
    return json;
}

void CRtModelYxInfo::fromJson(const QJsonObject &obj) {
    CRtModelSigBase::fromJson(obj);
    valuesFromJson(obj[yxFieldName(YX_ENUM::EValues)].toArray());
}

QSharedPointer<IModel> CRtModelYxInfo::createEmpty()
{

}

bool CRtModelYxInfo::sync(bool dir)
{
    QSharedPointer<IModel> info(this);
    //return CWebRequestApi::ins().changeYx(info);
}
